<!-- 日期快捷输入: 输入日期 M-d或MM-dd, 进行校验并补全, 得到 '当前年-06-01' -->

<input id="input_date" placeholder="格式:6-1或06-01">
<button onclick="check()">检查</button>
<span id="span_hint"></span>
<script>
	function check() {
		let hintSpan = document.querySelector("#span_hint");
		
		let date = getDate(document.querySelector("#input_date"));console.log(date)
		if (date != null) {
			hintSpan.innerHTML = "√";
			hintSpan.style.color = "green";
		} else {
			hintSpan.innerHTML = "请输入正确格式: 如6-1或06-01";
			hintSpan.style.color = "red";
		}
	}
	
	//检查输入的日期格式是否正确, 正确则返回完整日期, 错误返回null; 参数i: 输入框
	function getDate(i) {
		let dateStr = i.value;
		let regex = /^(0?[1-9]|1[0-2])-(0?[1-9]|[12]?[0-9]|3[01])$/;
		
		if (!regex.test(dateStr)) {
			return null;
		}
		
		let currentYear = new Date().getFullYear();
		let leapYear = ((currentYear % 100 == 0) && (currentYear % 400 == 0)) || (currentYear % 4 == 0);
		
		let data = dateStr.split("-");
		let month = parseInt(data[0]);
		let day = parseInt(data[1]);
		
		if (month == 2) {
			if (leapYear && day > 29) return null;
			if (!leapYear && day > 28) return null;
		} else if ([1, 3, 5, 7, 8, 10, 12].indexOf(month) != -1) {
			if (day > 31) return null;
		} else {
			if (day > 30) return null;
		}
		
		dateStr = currentYear + "-" + dateStr;
		return dateStr;
	}
</script>